package com.treasure.common.model;

import com.treasure.common.exception.ParameterException;

import java.util.Objects;

/**
 * @author sunhongmin
 * @date 2023/9/1 17:44
 * @description
 */
public class SessionUserHolder {

    private static final ThreadLocal<SessionUser> sessionUserThreadLocal = new InheritableThreadLocal<>();

    public static SessionUser getSessionUser() {
        return sessionUserThreadLocal.get();
    }

    public static SessionUser getAndCheckSessionUser() {
        SessionUser sessionUser = getSessionUser();
        if (Objects.isNull(sessionUser)) {
            throw new ParameterException("获取登录用户失败，请登录后重新操作");
        }
        return sessionUser;
    }

    public static void setSessionUser(SessionUser sessionUser) {
        sessionUserThreadLocal.set(sessionUser);
    }

    public static void clear() {
        sessionUserThreadLocal.remove();
    }

}
